
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#ifdef PKG_USING_U8G2
#include <u8g2_port.h>

u8g2_t u8g2;
#define OLED_PIN_RESET GET_PIN(I,4)
#define OLED_PIN_DC    GET_PIN(I,7)

void lgwsc_oled_init(void)
{
    // Initialization
    u8g2_Setup_sh1106_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_rt_hw_i2c, u8x8_rt_gpio_and_delay);
    
    u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_RESET, OLED_PIN_RESET);
    u8x8_SetPin(u8g2_GetU8x8(&u8g2), U8X8_PIN_DC, OLED_PIN_DC);
    
    u8g2_InitDisplay(&u8g2);
    u8g2_SetPowerSave(&u8g2, 0);

    /* full buffer example, setup procedure ends in _f */
    u8g2_ClearBuffer(&u8g2);
    u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
    //u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread");
    u8g2_DrawStr(&u8g2, 1, 10, "ART-Pi LGWSC v1.0");
    
    //u8g2_SetFont(&u8g2, u8g2_font_5x8_tr);
    u8g2_SetFont(&u8g2, u8g2_font_6x10_tr);
    u8g2_DrawStr(&u8g2, 1, 20, "-Transport : WiFi");
    u8g2_DrawStr(&u8g2, 1, 30, "-Radio Type: LoRa");
    //u8g2_DrawStr(&u8g2, 1, 25, "North Type: WiFi");
    u8g2_DrawStr(&u8g2, 1, 40, "-Temperture: -");
    u8g2_DrawStr(&u8g2, 1, 50, "-RF Packets: -");
    
    //u8g2_SetFont(&u8g2, u8g2_font_4x6_tr);
    //u8g2_DrawStr(&u8g2, 20, 60, "201230");
    
    u8g2_SendBuffer(&u8g2);
}

void lgwsc_oled_update(float temperature,uint32_t cp_nb_rx_rcv)
{
 /* full buffer example, setup procedure ends in _f */
    u8g2_ClearBuffer(&u8g2);
    u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
    //u8g2_DrawStr(&u8g2, 1, 18, "U8g2 on RT-Thread");
    u8g2_DrawStr(&u8g2, 1, 10, "ART-Pi LGWSC v1.0");
    
    //u8g2_SetFont(&u8g2, u8g2_font_5x8_tr);
    u8g2_SetFont(&u8g2, u8g2_font_6x10_tr);
    u8g2_DrawStr(&u8g2, 1, 20, "-Transport : WiFi");
    u8g2_DrawStr(&u8g2, 1, 30, "-Radio Type: LoRa");
    //u8g2_DrawStr(&u8g2, 1, 25, "North Type: WiFi");
    u8g2_DrawStr(&u8g2, 1, 40, "-Temperture:");
  
    char temperture_str[10];
    sprintf(temperture_str, "%.2f", temperature);
    u8g2_DrawStr(&u8g2, 80, 40, temperture_str); 
              
    char cp_nb_rx_rcv_strings[10];
    rt_sprintf(cp_nb_rx_rcv_strings, "%u", cp_nb_rx_rcv);
        
    u8g2_DrawStr(&u8g2, 1, 50, "-RF Packets:");
    u8g2_DrawStr(&u8g2, 80, 50, cp_nb_rx_rcv_strings);  

    //u8g2_DrawStr(&u8g2, 20, 60, "201230");

    u8g2_SendBuffer(&u8g2);
}
#endif
